﻿using Basic.HrDAL;
using Basic.HrModel.Msg;
using Basic.HrRemoteModel;
using Basic.HrRemoteModel.Msg.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.HrCollect.Impl
{
    internal class EmpMsgListCollect : IEmpMsgListCollect
    {
        private readonly IEmpMsgListDAL _BasicDAL;

        public EmpMsgListCollect ( IEmpMsgListDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public Result[] Query<Result> ( MsgQuery query, IBasicPage paging, out int count ) where Result : class
        {
            return this._BasicDAL.Query<Result>(query, paging, out count);
        }
        public void Adds ( MsgAdd add )
        {
            this._BasicDAL.Adds(add);
        }
        public Result Get<Result> ( long id ) where Result : class
        {
            return this._BasicDAL.Get<Result>(id);
        }
        public Result Get<Result> ( string linkType, long linkPk, string tag ) where Result : class, new()
        {
            if ( tag.IsNull() )
            {
                return this._BasicDAL.Get<Result>(a => a.LinkType == linkType && a.LinkPk == linkPk && a.ToVoid == false);
            }
            else
            {
                return this._BasicDAL.Get<Result>(a => a.LinkType == linkType && a.LinkPk == linkPk && a.Tag == tag && a.ToVoid == false);
            }
        }
        public bool SetAlreadyDo ( EmpMsgDto msg )
        {
            if ( msg.MsgStatus == HrMsgStatus.已办 )
            {
                return false;
            }
            this._BasicDAL.SetAlreadyDo(msg.Id);
            return true;
        }
        public bool SetAlreadyRead ( EmpMsgDto msg )
        {
            if ( msg.IsRead )
            {
                return false;
            }
            this._BasicDAL.SetAlreadyRead(msg.Id);
            return true;
        }
        public void SetAlreadyRead ( long[] ids )
        {
            this._BasicDAL.SetAlreadyRead(ids);
        }

        public long[] GetWaitToVoidMsgId ( ToVoidItem[] items )
        {
            return this._BasicDAL.GetWaitToVoidMsgId(items);
        }
        public long[] GetMsgId ( MsgItem[] items )
        {
            return this._BasicDAL.GetMsgId(items);
        }
        public void ToVoidMsg ( long[] msgId )
        {
            this._BasicDAL.ToVoidMsg(msgId);
        }

        public void ToVoidMsg ( string linkType, long linkPk, string tag )
        {
            long msgId;
            if ( tag.IsNull() )
            {
                msgId = this._BasicDAL.Get(a => a.LinkType == linkType && a.LinkPk == linkPk && a.ToVoid == false, a => a.Id);
            }
            else
            {
                msgId = this._BasicDAL.Get(a => a.LinkType == linkType && a.LinkPk == linkPk && a.Tag == tag && a.ToVoid == false, a => a.Id);
            }
            if ( msgId != 0 )
            {
                this._BasicDAL.ToVoidMsg(msgId);
            }
        }

        public void Save ( BatchOpt opt )
        {
            if ( opt.DoneId.IsNull() && opt.ToVoidId.IsNull() && opt.Adds.IsNull() )
            {
                return;
            }
            if ( opt.DoneId.IsNull() && opt.ToVoidId.IsNull() )
            {
                this._BasicDAL.Adds(opt.Adds);
                return;
            }
            this._BasicDAL.Save(opt);
        }

        public Dictionary<string, int> GetMsgTotal ( MsgGetArg arg )
        {
            return this._BasicDAL.GetMsgTotal(arg);
        }

        public TimeOutMsg[] GetTimeOuts ()
        {
            DateTime now = DateTime.Now;
            return this._BasicDAL.Gets<TimeOutMsg>(a => a.MsgType == HrMsgType.待办消息 && a.MsgStatus == HrMsgStatus.待办 && a.ToVoid == false && a.TimeOut != null && a.TimeOut < now);
        }
    }
}
